package com.xuhuan.client.component;

import feign.Response;
import feign.codec.ErrorDecoder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author huan.xu
 * @Date 2023/6/16 18:18
 * @Description open feign异常统一处理
 */
@Slf4j
@Component
public class FeignClientErrorDecoder implements ErrorDecoder {
    @Override
    public Exception decode(String s, Response response) {
        log.info("调用远程接口失败:接口[{}]", s);
        try {
//            if (response.body() != null) {
//                String errorContent = Util.toString(response.body().asReader());
//                Gson gson = new Gson();
//                if (response.status() == ErrorCode.INTERNAL_SERVER_ERROR) {
//                    return gson.fromJson(errorContent, ServerException.class);
//                } else if (response.status() == ErrorCode.PARAMETER_VALIDATION_ERROR) {
//                    return gson.fromJson(errorContent, ParamValidateException.class);
//                } else if (response.status() == ErrorCode.BUSINESS_EXCEPTION) {
//                    return gson.fromJson(errorContent, BusinessException.class);
//                }
//            }
            if (response.body() != null) {
                String errorContent = response.body().asReader().toString();
                int status = response.status();
                log.info("错误状态：{}，错误内容：{}", status, errorContent);
            }
        } catch (IOException e) {
            log.error("FeignClientErrorDecoder decode exception:{}.", e);
            return e;
        }
        return new Exception("服务端未知异常！");
    }
}
